<?php
require_once "Zend/Session/Namespace.php";
class IndexController extends Zend_Controller_Action
{

    private $_siteInfo = null;

    public function init()
    {
		$this->_siteInfo = new Zend_Session_Namespace('siteid');
		
		$application = new Zend_Application(APPLICATION_ENV,APPLICATION_PATH . '/configs/application.ini');
		$tabs = $application->getOption('tabs');
		$this->view->tabs = $tabs;
    }

    public function indexAction()
    {
		$req = $this->getRequest();
		if($req->isGet()){
			$tab = $req->get('tab');
			$this->view->tab = $tab;
		} else if ($req->isXMLHttpRequest()){//ajax
			$lat = $req->getPost('lat');
			$lon = $req->getPost('lon');
			$rest = new Application_Model_Rest();
			$result = $rest->getSiteList('','',$lat,$lon);
			
			if(!empty($result)){
				$oneRow = $result[0];
				$siteid = $oneRow->id;
				$domainName = $oneRow->domainname;
				$address = $oneRow->address;

				$this->_siteInfo->siteid = $siteid;
				$this->_siteInfo->domainName = $domainName;
				$this->_siteInfo->domainAddress = $address;

				echo json_encode(array('msg'=>$result));
				exit;
			}else{
				echo json_encode(array('msg'=>'noData'));
				exit;
			}
		} else {
			$this->_redirect('/address');
		}
		
    }

    public function homeAction()
    {
		$this->getHelper('layout')->disableLayout();
		$req = $this->getRequest();
		if (!$req->isXMLHttpRequest()) {
			$this->_redirect("/");
		} else {
			$tag = $req->getPost('tag');
			if(!empty($tag)&& $tag=='distributerClick'){
				$id = $req->getPost('id');
				$distributerName = $req->getPost('sitename');
				$distributerAddress = $req->getPost('address');
				$this->_siteInfo->distributerId = $id;
				$this->_siteInfo->distributerName = $distributerName;
				$this->_siteInfo->distributerAddress = $distributerAddress;
				if(!empty($this->_siteInfo->distributerName)){
					echo json_encode(array('msg'=>'sessionSuccess'));
					exit;
				}
			}else{
				$this->view->distributerId = '';
				$this->view->title = '请确认小区';
				$this->view->subTitle = '请确认小区地址';
				$siteid = $this->_siteInfo->siteid;
				$domainName = $this->_siteInfo->domainName;
				$domainAddress = $this->_siteInfo->domainAddress;
				
				
				$this->view->needAutoLocate = 'yes';
				if(($this->_siteInfo->siteid > 0 &&!empty($domainName)) || $this->view->tab == 'presale'){
					$this->view->needAutoLocate = 'no';
					$rest = new Application_Model_Rest();
					$siteMarket = $rest->getSiteMarket($siteid);
					if(!empty($siteMarket)){
						$this->view->siteid = $siteid;
						$this->view->domain = $domainName;
						$this->view->domain_address = $domainAddress;
						$this->view->adList = $siteMarket->advertise;
						$this->view->presell = $siteMarket->presell;
						$this->view->sectionList = $this->objToArr($siteMarket->sectionList);
						$this->view->hotBrandList = isset($siteMarket->hotBrandList) ? $siteMarket->hotBrandList : array();
						$this->view->needUpdateDistributer = false;
						$this->view->distributerList = $siteMarket->distributerList;
						$this->view->title = $domainName;
						$this->view->subTitle = $domainAddress;
						if (!empty($siteMarket->distributerList)) {
							$found = false;
							foreach($siteMarket->distributerList as $distributer) {
								if ( 0 == $this->_siteInfo->distributerId - $distributer->id) {
									$found = true;
									$this->_siteInfo->distributerName = $distributer->sitename;
									$this->_siteInfo->distributerAddress = $distributer->address;
									break;
								}
							}
							if (false == $found) {
								$this->_siteInfo->distributerId = $siteMarket->distributerList[0]->id;
								$this->_siteInfo->distributerName = $siteMarket->distributerList[0]->sitename;
								$this->_siteInfo->distributerAddress = $siteMarket->distributerList[0]->address;
								$this->view->needUpdateDistributer = true;
							}
							$this->view->distributerId = $this->_siteInfo->distributerId;
							$this->view->title = $this->_siteInfo->distributerName;
							$this->view->subTitle = $this->_siteInfo->distributerAddress;
						}
						if(!empty($siteMarket->presell)){
							$obj=$siteMarket->presell[0];
							$this->_siteInfo->shopid=$obj->id;
						}else{
							$this->_siteInfo->shopid='';
						}
					}
				}
			}
		}
    }

    public function categroyAction()
    {
       $this->getHelper('layout')->disableLayout();
	   $req = $this->getRequest();
		if (!$req->isXMLHttpRequest()) {
			$this->_redirect("/");
		} else {
			$bid = $req->get('bid');
			if(!empty($bid)){
				$this->view->bid = $bid;
			}
			$this->view->distributerId = '';
			$this->view->title = '请确认小区';
			$this->view->subTitle = '请确认小区地址';
			$this->view->needUpdateDistributer = false;
			$shopId = $this->_siteInfo->shopid;
			if(!empty($shopId)){
				$rest = new Application_Model_Rest();
				$list = $rest->getUserShopProductList($shopId);
				$siteid = $this->_siteInfo->siteid;
				$this->view->distributerList='';
				$this->view->domain = $this->_siteInfo->domainName;
				$this->view->title = $this->_siteInfo->domainName;
				$this->view->subTitle = $this->_siteInfo->domainAddress;
				if(!empty($siteid)){
					$siteMarket = $rest->getSiteMarket($siteid);
					if(!empty($siteMarket)){
						$this->view->distributerList = $siteMarket->distributerList;
						if (!empty($siteMarket->distributerList)) {
							$found = false;
							foreach($siteMarket->distributerList as $distributer) {
								if ( 0 == $this->_siteInfo->distributerId - $distributer->id) {
									$found = true;
									$this->_siteInfo->distributerName = $distributer->sitename;
									$this->_siteInfo->distributerAddress = $distributer->address;
									break;
								}
							}
							if (false == $found) {
								$this->_siteInfo->distributerId = $siteMarket->distributerList[0]->id;
								$this->_siteInfo->distributerName = $siteMarket->distributerList[0]->sitename;
								$this->_siteInfo->distributerAddress = $siteMarket->distributerList[0]->address;
								$this->view->needUpdateDistributer = true;
							}
							$this->view->distributerId = $this->_siteInfo->distributerId;
							$this->view->title = $this->_siteInfo->distributerName;
							$this->view->subTitle = $this->_siteInfo->distributerAddress;
						}
					}
				}
				$arr_brand = array();
				$arr_productList = array();
				foreach($list as $row){
					if (!array_key_exists($row->brandid,$arr_brand)) {
						$arr_brand[$row->brandid] = $row->brandname;
					}
					$arr_productList[$row->brandid][] = $row;
				}
				ksort($arr_brand);
				ksort($arr_productList);
				$this->view->arr_brand = $arr_brand;
				$this->view->arr_productList = $arr_productList;
				$this->view->shopid = $shopId;
				$obj = $rest->getShopDetails($shopId);
				$this->view->obj=$obj;
			}else{
				echo 'shopidIsNull';
				exit;
			}
		}
    }

    public function personalAction()
    {
       $this->getHelper('layout')->disableLayout();
	   $req = $this->getRequest();
		if (!$req->isXMLHttpRequest()) {
			$this->_redirect("/");
		}else{
			$auth = new Application_Model_Auth();
			$rest = new Application_Model_Rest();
			if(!$auth->isLoggedIn()){
				$rurl = '/personal';
				$this->_redirect('/login?rurl='.$rurl);
			}
			$res_info = $rest->getUser();
			$this->view->res_info = $res_info;
			$tel = $auth->getUserName();
			$this->view->tel = $tel;
			$shopId = $this->_siteInfo->shopid;
			if(empty($shopId)){
				$this->view->shopidIsNull="shopidIsNull";
			}
		}
	  
    }

    public function cartAction()
    {
	   $this->getHelper('layout')->disableLayout();
	   $req = $this->getRequest();
		if (!$req->isXMLHttpRequest()) {
			$this->_redirect("/");
		}else{
			$shopId = $this->_siteInfo->shopid;
			if(!empty($shopId)){
				$rest = new Application_Model_Rest();
				$this->view->shopid = $shopId;
				$obj = $rest->getShopDetails($shopId);
				$this->view->obj=$obj[0];
				$auth = new Application_Model_Auth();
				if(!$auth->isLoggedIn()){
					$rurl = '/cart';
					$this->_redirect('/login?rurl='.$rurl);
				}
				$cartCountPriceStr =$req->get('cart');
				
				//$acList  =  1:1,5;2,3|2:5,1;12,7     0:37,2;966,1;2,2
				$arr = explode(',',$cartCountPriceStr);
				$cartList = array();
				for($i=0;$i<count($arr)/4;$i++){
					$j = $i * 4;
					$cartList[] = array($arr[$j],$arr[$j+1],$arr[$j+2],$arr[$j+3]);
				}
				$acList = $shopId.':';
				foreach($cartList as $order) {
					$acList .= $order[1].','.$order[3].';';
				}
				$acList = rtrim($acList,';');
				$resCartList = $rest->getUserCartDetails($acList);
				$this->view->resCartList = $resCartList;
			}else{
				echo 'shopidIsNull';
				exit;
			}
		}
    }

    public function presaleAction()
    {
		$this->getHelper('layout')->disableLayout();
		$req = $this->getRequest();
		if (!$req->isXMLHttpRequest()) {
			$this->_redirect("/");
		}else{
			$rest = new Application_Model_Rest();
			$siteid = $this->_siteInfo->siteid;
			$this->view->productLists = $rest->getUserPresaleShopProductList($cityId=25,$siteid);
		}
    }

    public function objToArr(&$object)
    {
             $object =  json_decode( json_encode( $object),true);
             return  $object;
    }

    public function clickAction()
    {
       $req = $this->getRequest();
	   if($req->isPost()){
		   $action = $req->getPost('actionStr');
		   $actionArr = base64_decode($action);
		   $actionArr = unserialize($actionArr);
		   $actionId = $actionArr['id'];
		   $parameter = $actionArr['parameter'];
		   if($actionId=='3'){
			   $rest = new Application_Model_Rest();
			   $shopId = $parameter['shopId'];
			   $productId = $parameter['productId'];
			   $shopProduct = $rest->getShopProductByShopIdAndProductId($shopId,$productId);
			   $shopProductId = $shopProduct->id;
			   echo json_encode(array('msg'=>'success','actionId'=>$actionId,'shopProductId'=>$shopProductId));
			   exit;
		   }else{
			   echo json_encode(array('msg'=>'success','actionId'=>$actionId,'parameter'=>$parameter));
			   exit;
		   }
	   }
    }


}


